package com.dream.testtubebaby_doctor.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.dream.testtubebaby_doctor.R;
import com.dream.testtubebaby_doctor.data.AddToldData;

public class WaitingRoomExpandableListAdapter extends BaseExpandableListAdapter
		implements Filterable {
	private Context mContext;
	private HashMap<String, List<AddToldData>> mGroupData;
	private ItemInfoFilter filter;

	private class ItemInfoFilter extends Filter {

		protected android.widget.Filter.FilterResults performFiltering(
				CharSequence charsequence) {
			android.widget.Filter.FilterResults filterresults = new android.widget.Filter.FilterResults();
			String s = charsequence.toString().replaceAll(" ", "")
					.toLowerCase().trim();
			if (charsequence == null || charsequence.length() == 0) {
				filterresults.values = original;
				filterresults.count = original.size();
			} else {
				HashMap<String, List<AddToldData>> arraylist = new HashMap<String, List<AddToldData>>();
				Object[] keys = original.keySet().toArray();
				for (int i = 0; i < keys.length; i++) {
					if (keys[i] != null
							&& ((String) keys[i]).toLowerCase()
									.replaceAll(" ", "").startsWith(s)) {
						arraylist.put((String) keys[i], original.get(keys[i]));
					}
				}
				filterresults.values = arraylist;
				filterresults.count = arraylist.size();
			}
			return filterresults;
		}

		@SuppressWarnings("unchecked")
		protected void publishResults(CharSequence charsequence,
				android.widget.Filter.FilterResults filterresults) {
			mGroupData = (HashMap<String, List<AddToldData>>) filterresults.values;
			notifyDataSetChanged();
		}

		private HashMap<String, List<AddToldData>> original;

		public ItemInfoFilter(HashMap<String, List<AddToldData>> list) {
			original = list;
		}
	}

	public Filter getFilter() {
		if (filter == null)
			filter = new ItemInfoFilter(mGroupData);
		return filter;
	}

	public WaitingRoomExpandableListAdapter(Context context,
			HashMap<String, List<AddToldData>> groupData) {
		super();
		mContext = context;
		mGroupData = groupData;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return mGroupData.get(getGroup(groupPosition)).get(childPosition);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		return arg1;
	}

	private class ChildViewHolder {

		TextView wait_username_tv;
		TextView wait_complete_time_tv;
		TextView wait_project_tv;
		TextView wait_category_tv;
		TextView wait_quantity_tv;

		public ChildViewHolder(View view) {
			wait_username_tv = (TextView) view
					.findViewById(R.id.wait_username_tv);
			wait_complete_time_tv = (TextView) view
					.findViewById(R.id.wait_complete_time_tv);
			wait_project_tv = (TextView) view
					.findViewById(R.id.wait_project_tv);
			wait_category_tv = (TextView) view
					.findViewById(R.id.wait_category_tv);
			wait_quantity_tv = (TextView) view
					.findViewById(R.id.wait_quantity_tv);
		}
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflaterGroup = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflaterGroup.inflate(
					R.layout.waiting_room_list_child_item, null);
			ChildViewHolder holder = new ChildViewHolder(convertView);
			convertView.setTag(holder);
		}
		ChildViewHolder holder = (ChildViewHolder) convertView.getTag();

		AddToldData addToldData = (AddToldData) getChild(groupPosition,
				childPosition);
		holder.wait_username_tv.setText(addToldData.getUsername());
		CharSequence time_string = DateUtils.getRelativeTimeSpanString(
				addToldData.getFinishtime(), System.currentTimeMillis(),
				DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);
		holder.wait_complete_time_tv.setText(time_string);
		holder.wait_project_tv.setText(addToldData.getStepname());
		holder.wait_category_tv.setText(addToldData.getDrug());
		holder.wait_quantity_tv.setText(addToldData.getHowmuch());
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mGroupData.get(getGroup(groupPosition)).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mGroupData.keySet().toArray()[groupPosition];
	}

	@Override
	public int getGroupCount() {
		return mGroupData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	private class GroupViewHolder {

		TextView wait_group_username_tv;
		ImageView arrow_iv;

		public GroupViewHolder(View view) {
			wait_group_username_tv = (TextView) view
					.findViewById(R.id.wait_group_username_tv);
			arrow_iv = (ImageView) view.findViewById(R.id.arrow_iv);
		}
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflaterGroup = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflaterGroup.inflate(
					R.layout.waiting_room_list_group_item, null);
			GroupViewHolder holder = new GroupViewHolder(convertView);
			convertView.setTag(holder);

		}
		GroupViewHolder holder = (GroupViewHolder) convertView.getTag();
		holder.wait_group_username_tv.setText((String) getGroup(groupPosition));
		if (isExpanded) {
			holder.arrow_iv
					.setBackgroundResource(R.drawable.ic_collapse_small_holo_light);
		} else {
			holder.arrow_iv
					.setBackgroundResource(R.drawable.ic_expand_small_holo_light);
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

}
